package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Member;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private UserDao userDao;

    //根据手机号查询会员
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }
    

    //根据月份统计会员数量
    @Override
    public List<Integer> findMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for(String m : month){
            m = m + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }
    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void edit(Member member) {
        if (member.getManager()!=null){
            long count=memberDao.findCountByMemberId(member.getId());
            if (count>0){
                memberDao.deleteAssociation(member.getId());
            }
            Integer uid = userDao.findUserIdByManager(member.getManager());
            memberDao.setMemberAndUser(member.getId(),uid);
        }
        if(member.getBirthday() != null){
            member.setAge(new Date().getYear() - member.getBirthday().getYear());
        }
        memberDao.edit(member);
    }

    @Override
    public void delete(Integer id) {
        long orderCount = orderDao.findCountByMemberId(id);
        if (orderCount == 0) {
            long userCount = memberDao.findCountByMemberId(id);
            if (userCount>0){
                memberDao.deleteAssociation(id);
            }
            memberDao.delete(id);
        } else {
            throw new RuntimeException("会员信息被引用,不能删除");
        }
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Map<String, Object>> names() {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<String> names = memberDao.names();
        for (String name : names) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", name);
            mapList.add(map);
        }
        return mapList;
    }



    @Override
    public void add(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        if(member.getBirthday() != null){
            member.setAge(new Date().getYear() - member.getBirthday().getYear());
        }
        member.setRegTime(new Date());
        memberDao.add(member);
        Integer memberId = member.getId();
        if (member.getManager() != null) {
            Integer uid = userDao.findUserIdByManager(member.getManager());
            memberDao.setMemberAndUser(memberId, uid);
        }
    }
    
}
